//
//  HPShareDetailsHeaderView.m
//  fruitsDealers
//
//  Created by sgone on 16/4/20.
//  Copyright © 2016年 com.sgone. All rights reserved.
//

#import "HPShareDetailsHeaderView.h"
#import "ShareListImageView.h"
#import "FDLineView.h"
#import "UIView+Helper.h"
#import "labelAdaptive.h"
@interface HPShareDetailsHeaderView ()<ShareListImageViewDelegate>
{
    
    
}

/**
 发布内容视图
 */
@property (nonatomic, strong) UIView *releaseView;
/**
 回复内容视图
 */
@property (nonatomic, strong) UIView *replyView;
/**
 发布内容下划线
 */
@property (nonatomic, strong) FDLineView *releaseLineView;
/**
 回复内容视图下划线
 */
@property (nonatomic, strong) FDLineView *replyLineView;
/**
 发布用户头像
 */
@property (nonatomic, strong) UIImageView *iconImageView;
/**
 发布地址图片
 */
@property (nonatomic, strong) UIImageView *releaseImageView;
/**
 回复图片
 */
@property (nonatomic, strong) UIImageView *replyImageView;
/**
 发布用户名称
 */
@property (nonatomic, strong) UILabel *releaseNameLabel;
/**
 发布时间
 */
@property (nonatomic, strong) UILabel *releaseTimeLabel;
/**
 发布内容
 */
@property (nonatomic, strong) UILabel *releaseContentLabel;
/**
 发布人地址
 */
@property (nonatomic, strong) UILabel *releaseAddressLabel;
/**
 *评论图片
 */
@property (nonatomic, strong) ShareListImageView *listImageView;

@end

@implementation HPShareDetailsHeaderView

- (UIView *)releaseView {
    
    if (_releaseView == nil) {
        
        _releaseView = [[UIView alloc] init];
        [self addSubview:_releaseView];
    }
    return _releaseView;
}

- (FDLineView *)releaseLineView {
    
    if (_releaseLineView == nil) {
        
        _releaseLineView = [[FDLineView alloc] init];
        [self.releaseView addSubview:_releaseLineView];
    }
    return _releaseLineView;
}

- (UIImageView *)iconImageView {
    
    if (_iconImageView == nil) {
        
        _iconImageView = [[UIImageView alloc] init];
        _iconImageView.layer.masksToBounds = YES;
        _iconImageView.layer.cornerRadius = _iconImageView.frame.size.height / 2;
        [self.releaseView addSubview:_iconImageView];
    }
    return _iconImageView;
}

- (UILabel *)releaseNameLabel {
    
    if (_releaseNameLabel == nil) {
        
        _releaseNameLabel = [[UILabel alloc] init];
        _releaseNameLabel.textColor = FDFont_COLOR;
        _releaseNameLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
        [self.releaseView addSubview:_releaseNameLabel];
    }
    return _releaseNameLabel;
}

- (UILabel *)releaseTimeLabel {
    
    if (_releaseTimeLabel == nil) {
        
        _releaseTimeLabel = [[UILabel alloc] init];
        _releaseTimeLabel.textColor = FDGFont_COLOR;
        _releaseTimeLabel.font = [UIFont systemFontOfSize:12 / FDLaOt];
        [self.releaseView addSubview:_releaseTimeLabel];
    }
    return _releaseTimeLabel;
}

- (UILabel *)releaseContentLabel {
    
    if (_releaseContentLabel == nil) {
        
        _releaseContentLabel = [[UILabel alloc] init];
        _releaseContentLabel.textColor = FDMFont_COLOR;
        _releaseContentLabel.font = [UIFont systemFontOfSize:13 / FDLaOt];
        _releaseContentLabel.numberOfLines = 0;
        [self.releaseView addSubview:_releaseContentLabel];
    }
    return _releaseContentLabel;
}

- (UILabel *)releaseAddressLabel {
    
    if (_releaseAddressLabel == nil) {
        
        _releaseAddressLabel = [[UILabel alloc] init];
        _releaseAddressLabel.textColor = FDGFont_COLOR;
        _releaseAddressLabel.font = [UIFont systemFontOfSize:12 / FDLaOt];
        [self.releaseView addSubview:_releaseAddressLabel];
    }
    return _releaseAddressLabel;
}

- (UIImageView *)releaseImageView {
    
    if (_releaseImageView == nil) {
        
        _releaseImageView = [[UIImageView alloc] init];
        //        _releaseImageView.backgroundColor = [UIColor cyanColor];
        _releaseImageView.contentMode = UIViewContentModeCenter;
        [self.releaseView addSubview:_releaseImageView];
    }
    return _releaseImageView;
}

- (UIImageView *)replyImageView {
    
    if (_replyImageView == nil) {
        
        _replyImageView = [[UIImageView alloc] init];
        //        _replyImageView.backgroundColor = [UIColor cyanColor];
        _replyImageView.contentMode = UIViewContentModeCenter;
        [self.releaseView addSubview:_replyImageView];
    }
    return _replyImageView;
}

- (UIView *)replyView {
    
    if (_replyView == nil) {
        
        _replyView = [[UIView alloc] init];
        [self addSubview:_replyView];
    }
    return _replyView;
}

- (FDLineView *)replyLineView {
    
    if (_replyLineView == nil) {
        
        _replyLineView = [[FDLineView alloc] init];
        [self.replyView addSubview:_replyLineView];
    }
    return _replyLineView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

+ (instancetype)initWithFrame:(CGRect)frame {
    
    return [[self alloc] initWithFrame:frame];
}


- (CGFloat)cellHeightWithMessage:(NSDictionary *)messageDic
{
    
    self.iconImageView.image = [UIImage imageNamed:@"longin_default"];
    NSString *returnStr = [checkNull(messageDic[@"userinfo"][@"name"]) length] >0 ? messageDic[@"userinfo"][@"name"] : @"" ;
    
    self.releaseNameLabel.text = returnStr;
                           

    self.releaseTimeLabel.text = [self compareCurrentTime:[NSDate dateWithTimeIntervalSince1970:[messageDic[@"inputtime"] integerValue]]];
    
    self.releaseContentLabel.text = messageDic[@"body"];
    self.releaseImageView.image = [UIImage imageNamed:@"location"];
    
    
    
    NSString *province = [self getServiceStringAndReturnString:messageDic[@"userinfo"][@"province"]];
    
    NSString *city = [self getServiceStringAndReturnString:messageDic[@"userinfo"][@"city"]];
    
    NSString *town = [self getServiceStringAndReturnString:messageDic[@"userinfo"][@"town"]];
    
    
    if ([province isEqualToString:city]) {
        
        self.releaseAddressLabel.text = [NSString stringWithFormat:@"%@%@",province,town];
    }
    else
        self.releaseAddressLabel.text = [NSString stringWithFormat:@"%@%@%@",province,city,town];
    
   
        
    self.replyImageView.image = [UIImage imageNamed:@"回复"];
    
    self.iconImageView.frame = CGRectMake(8, 16, 34 / FDLaOt, 34 / FDLaOt);
    
    self.releaseNameLabel.frame = CGRectMake(self.iconImageView.x + self.iconImageView.width + 8, self.iconImageView.y, ViewWidth - self.iconImageView.width - self.iconImageView.x * 4, 18 / FDLaOt);
    
    self.releaseTimeLabel.frame = CGRectMake(self.iconImageView.x + self.iconImageView.width + 8, self.releaseNameLabel.y + self.releaseNameLabel.height, ViewWidth - self.iconImageView.width - self.iconImageView.x * 4, 16 / FDLaOt);
    
    NSLog(@"%@",self.releaseContentLabel.text);
    CGSize releaseContentSize = [labelAdaptive sizeWithString:self.releaseContentLabel.text font:self.releaseContentLabel.font];
    
    self.releaseContentLabel.frame = CGRectMake(self.iconImageView.x, self.iconImageView.y + self.iconImageView.height + 8, ViewWidth - self.iconImageView.x * 2, releaseContentSize.height);
    
    if ([messageDic[@"body"] length] == 0) {
        
        self.releaseContentLabel.frame = CGRectMake(self.iconImageView.x, self.iconImageView.y + self.iconImageView.height + 8, ViewWidth - self.iconImageView.x * 2, 0);
    }
    
    CGSize imagesSize;
    
    
    

    
    if (self.images.count > 0) {
        
        self.listImageView = [[ShareListImageView alloc] init];
        self.listImageView.images = self.images;
        self.listImageView.imagesDelegate = self;
        imagesSize = [self.listImageView sizeWithCount:(int)self.images.count];
        
        if ([messageDic[@"body"] length] == 0) {
            
            self.listImageView.frame = CGRectMake(self.iconImageView.x, self.iconImageView.y + self.iconImageView.height + 8, imagesSize.width, imagesSize.height);
        } else
            
            self.listImageView.frame = CGRectMake(self.iconImageView.x, self.releaseContentLabel.y + self.releaseContentLabel.height + 8, imagesSize.width, imagesSize.height);
        [self.releaseView addSubview:self.listImageView];
        
        self.releaseImageView.frame = CGRectMake(self.iconImageView.x, self.listImageView.y + imagesSize.height + 8, 20 / FDLaOt, 20 / FDLaOt);
    } else {
        
        self.releaseImageView.frame = CGRectMake(self.iconImageView.x, self.releaseContentLabel.y + self.releaseContentLabel.height + 8, 20 / FDLaOt, 20 / FDLaOt);
    }
    
    self.replyImageView.frame = CGRectMake(ViewWidth - 8 - self.releaseImageView.width, self.releaseImageView.y, self.releaseImageView.width, self.releaseImageView.height);
    
    self.releaseAddressLabel.frame = CGRectMake(self.releaseImageView.x + self.releaseImageView.width + 8, self.releaseImageView.y, ViewWidth - self.releaseImageView.width - self.replyImageView.width - 4 * 8, 20 / FDLaOt);
    
    self.releaseView.frame = CGRectMake(0, 0, ViewWidth, self.releaseImageView.y + self.releaseImageView.height + 8);
    
    
    self.releaseLineView.frame = CGRectMake(self.iconImageView.x, self.releaseView.height - 0.5, ViewWidth - self.iconImageView.x * 2, 0.5);
    
    return self.releaseView.height;
}

- (NSString *)compareCurrentTime:(NSDate *)compareDate {
    
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
}

- (NSString *)getServiceStringAndReturnString:(NSString *)string {
    
    NSString *returnStr = [checkNull(string) length] > 0 ? string : @"";
    
    return returnStr;
}

- (void)imageWithName:(NSMutableArray *)array andIndex:(int)index {
    
    [self.delegate imageWithNames:array andIndex:index];
}

@end
